package connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class Login {
	private String mid="";
	private String pwd="";
	private String NewPwd1="";
	private String pwd1="";
	private String irum="";
	private String birth="";
	private String gender="";
	private String addr1="";
	private String addr2="";
	private String addrTot="";
	private String pnum1="";
	private String pnum2="";
	private String pnum3="";
	private String pnumTot="";
	private String num1="";
	private String num2="";
	private String num3="";
	private String numTot="";
	private String email1="";
	private String email2="";
	private String email3="";
	private String emailTot="";
	private String care="";
	private String care1="";
	private String care2="";
	private String care3="";
	private String care4="";
	private String care5="";
	private String care6="";
	private String care7="";
	private String care8="";
	private String care9="";

	private String careTxt="";//기타
	private String intro="";//텍스트 에어리어
	
	String driver="oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@192.168.10.122:1521:XE";
	String user = "jobs";
	String pwd2 = "1234";	

	
	Connection conn;
	
	public Login(){	
		try{
			Class.forName(driver).newInstance();
			conn=DriverManager.getConnection(url, user, pwd2);
			
		}catch(Exception ex){
			System.out.print(ex.toString());	
		}
	}
	
	public boolean login(){
		boolean flag=false;
		try{
			String sql="select mid, pwd from member_register where mid=? and pwd=?";
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,mid);
			ps.setString(2,pwd);
			
			ResultSet rs=ps.executeQuery();
			
			//회원인경우
			if(rs.next()){
				flag=true;
			}else{
				flag=false;
			}
			rs.close();ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return flag;
	}
	public boolean join(){
		boolean flag=false;		
		care=care1+care2+care3+care4+care5+care6+care7+care8+care9;		
		
		try{
			String sql="insert into member_register "
			+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,mid);
			ps.setString(2,pwd);
			ps.setString(3,irum);
			
			ps.setString(4,gender);
			ps.setString(5,addr1+addr2);
			ps.setString(6,pnum1+"-"+pnum2+"-"+pnum3);
			ps.setString(7,num1+"-"+num2+"-"+num3);
			ps.setString(8,email1+"@"+email2);
			ps.setString(9,care);			
			ps.setString(10,intro);
			ps.setString(11, careTxt);
			ps.setString(12, birth);
			ResultSet rs=ps.executeQuery();			
			
			if(rs.next()){
				flag=true;
			}else{
				flag=false;
			}
			rs.close();ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return flag;
	}
	
	public String getCare() {
		return care;
	}

	public void setCare(String care) {
		this.care = care;
	}

	public int idCheck(String id){
		  int rst = 0;		  
		  PreparedStatement ps = null;
		  ResultSet rs = null;
		  try{		
		   String sql = "select mid from member_register where mid=?";
		   ps = conn.prepareStatement(sql);
		   ps.setString(1, id);
		   rs = ps.executeQuery();
		   if(rs.next()){
			   rst = 1;
		   }else{
			   rst=0;
		   }
			rs.close();ps.close();conn.close();
		   }catch(Exception e){
		   e.printStackTrace();
		  }
		  return rst;
	}
	//패스워드 수정
	public boolean modify(){
		boolean flag=false;
		String sql="update member_register set pwd=? where mid=? and pwd=?";
		try{
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, NewPwd1);
			ps.setString(2, mid);
			ps.setString(3, pwd);
			
			int cnt=ps.executeUpdate();
			if(cnt>0) flag=true;
			
			ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return flag;
	}
	//패스워드 수정시 아이디로 패스워드 찾기
	public MemberVo view(String mid){
		MemberVo m=new MemberVo();
		String sql="select * from member_register where mid=?";
		try{
			PreparedStatement ps=conn.prepareStatement(sql); //mid 주소값을 가져올 것임
			ps.setString(1, mid);
			ResultSet rs=ps.executeQuery();
			
			if(rs.next()){
				m.setPwd(rs.getString("pwd"));
			}
			
			rs.close();ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return m;
	}
	
	//회원정보 수정시 아이디로 회원정보를 모두 가져온다
	public Login viewAll(String mid){
		Login m=new Login();
		String sql="select * from member_register where mid=?";
		try{
			PreparedStatement ps=conn.prepareStatement(sql); //mid 주소값을 가져올 것임
			ps.setString(1, mid);
			ResultSet rs=ps.executeQuery();
			
			if(rs.next()){
				m.setAddrTot(rs.getString("address"));//주소
				m.setPnumTot(rs.getString("phone_number"));//핸드폰번호
				m.setNumTot(rs.getString("home_number"));//전화번호
				m.setEmailTot(rs.getString("email"));//이메일
				m.setCare(rs.getString("hobby"));
				m.setCareTxt(rs.getString("ect"));
				m.setIntro(rs.getString("intro"));//텍스트 에어리어
				m.setPwd(rs.getString("pwd"));
			}
			
			rs.close();ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return m;
	}

	//아이디 값을 가져온다
	public Login searchId(){
		Login m=new Login();
		String sql="select mid from member_register where irum=? and birth=? and email=?";
		try{
			PreparedStatement ps=conn.prepareStatement(sql); //mid 주소값을 가져올 것임
			ps.setString(1, irum);
			ps.setString(2, birth);
			ps.setString(3, emailTot);
			ResultSet rs=ps.executeQuery();
			
			if(rs.next()){
				m.setMid(rs.getString("mid"));
			}
			rs.close();ps.close();conn.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return m;
	}
	
	public Login searchpwd(){
		Login m=new Login();
		String sql="select pwd, email, mid from member_register where mid=? and birth=? and email=?";
		try{
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, birth);
			ps.setString(3, emailTot);
			ResultSet rs=ps.executeQuery();
			
			if(rs.next()){
				m.setPwd(rs.getString("pwd"));
				m.setMid(rs.getString("mid"));
				m.setEmailTot(rs.getString("email"));
			}
			
			rs.close();ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return m;
	}
	public boolean drop(){//회원 탈퇴
		boolean flag=false;
		String sql="delete from member_register where mid=? and pwd=? and irum=? and birth=? and email=?";
		try{
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, pwd);
			ps.setString(3, irum);
			ps.setString(4, birth);
			ps.setString(5, (email1+"@"+email2));
			
			int cnt=ps.executeUpdate();
			if(cnt>0) flag=true;
			
			ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return flag;
	}
	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	//회원정보 수정
	public boolean InforModify(){
		boolean flag=false;		
		care=care1+care2+care3+care4+care5+care6+care7+care8+care9;		
		
		try{
			String sql="update member_register set address=? , phone_number=? , home_number=? "
					+ ", email=? , hobby=? , intro=? , ect=?"
					+ " where mid=?";
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,addr1+addr2);
			ps.setString(2,pnum1+"-"+pnum2+"-"+pnum3);
			ps.setString(3,num1+"-"+num2+"-"+num3);
			ps.setString(4,email1+"@"+email2);
			ps.setString(5,care);			
			ps.setString(6,intro);
			ps.setString(7,careTxt);
			ps.setString(8, mid);
			
			ResultSet rs=ps.executeQuery();			
			
			if(rs.next()){
				flag=true;
			}else{
				flag=false;
			}
			rs.close();ps.close();conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return flag;
	}

	public String getEmailTot() {
		return emailTot;
	}

	public void setEmailTot(String emailTot) {
		this.emailTot = emailTot;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String getPwd1() {
		return pwd1;
	}

	public void setPwd1(String pwd1) {
		this.pwd1 = pwd1;
	}

	public String getIrum() {
		return irum;
	}

	public void setIrum(String irum) {
		this.irum = irum;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAddr1() {
		return addr1;
	}

	public void setAddr1(String addr1) {
		this.addr1 = addr1;
	}

	public String getAddr2() {
		return addr2;
	}

	public void setAddr2(String addr2) {
		this.addr2 = addr2;
	}

	public String getPnum1() {
		return pnum1;
	}

	public void setPnum1(String pnum1) {
		this.pnum1 = pnum1;
	}

	public String getPnum2() {
		return pnum2;
	}

	public void setPnum2(String pnum2) {
		this.pnum2 = pnum2;
	}

	public String getPnum3() {
		return pnum3;
	}

	public void setPnum3(String pnum3) {
		this.pnum3 = pnum3;
	}

	public String getNum1() {
		return num1;
	}

	public void setNum1(String num1) {
		this.num1 = num1;
	}

	public String getNum2() {
		return num2;
	}

	public void setNum2(String num2) {
		this.num2 = num2;
	}

	public String getNum3() {
		return num3;
	}

	public void setNum3(String num3) {
		this.num3 = num3;
	}

	public String getEmail1() {
		return email1;
	}

	public void setEmail1(String email1) {
		this.email1 = email1;
	}

	public String getEmail2() {
		return email2;
	}

	public void setEmail2(String email2) {
		this.email2 = email2;
	}

	public String getEmail3() {
		return email3;
	}

	public void setEmail3(String email3) {
		this.email3 = email3;
	}
	public String getCareTxt() {
		return careTxt;
	}

	public void setCareTxt(String careTxt) {
		this.careTxt = careTxt;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}
	public String getCare6() {
		return care6;
	}

	public void setCare6(String care6) {
		this.care6 = care6;
	}

	public String getCare7() {
		return care7;
	}

	public void setCare7(String care7) {
		this.care7 = care7;
	}

	public String getCare1() {
		return care1;
	}

	public void setCare1(String care1) {
		this.care1 = care1;
	}

	public String getCare2() {
		return care2;
	}

	public void setCare2(String care2) {
		this.care2 = care2;
	}

	public String getCare3() {
		return care3;
	}

	public void setCare3(String care3) {
		this.care3 = care3;
	}

	public String getCare4() {
		return care4;
	}

	public void setCare4(String care4) {
		this.care4 = care4;
	}

	public String getCare5() {
		return care5;
	}

	public void setCare5(String care5) {
		this.care5 = care5;
	}
	
	public static void main(String[] args){
		Login l=new Login();
	}
	public String getNewPwd1() {
		return NewPwd1;
	}

	public void setNewPwd1(String newPwd1) {
		NewPwd1 = newPwd1;
	}
	

	public String getCare8() {
		return care8;
	}

	public void setCare8(String care8) {
		this.care8 = care8;
	}
	public String getCare9() {
		return care9;
	}

	public void setCare9(String care9) {
		this.care9 = care9;
	}
	public String getPnumTot() {
		return pnumTot;
	}

	public void setPnumTot(String pnumTot) {
		this.pnumTot = pnumTot;
	}

	public String getNumTot() {
		return numTot;
	}

	public void setNumTot(String numTot) {
		this.numTot = numTot;
	}
	
	public String getAddrTot() {
		return addrTot;
	}

	public void setAddrTot(String addrTot) {
		this.addrTot = addrTot;
	}
}
